<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="报告标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入报告标题"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建人姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间段">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- =========================================== -->
    <el-row :gutter="10" style="margin-bottom: 20px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <!-- 表格数据 -->
    <el-table
      :data="tableData"
      stripe
      v-loading="loading"
      border
      style="margin-bottom: 20px"
      :default-sort="{ prop: 'date', order: 'descending' }"
    >
      <el-table-column
        label="报告标题"
        prop="title"
        align="center"
      ></el-table-column>
      <el-table-column
        label="创建人"
        prop="name"
        align="center"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        prop="createTime"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        label="更新时间"
        prop="createTime"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column label="报告状态" align="center">
        <template v-slot="scope">
          <el-tag type="danger" v-if="scope.row.result == null">未处理</el-tag>
          <el-tag type="success" v-if="scope.row.result != null">已处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报告处理结果" align="center">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.result == '0'">通过</el-tag>
          <el-tag type="danger" v-if="scope.row.result == '1'">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="300"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="update(scope.row)"
            v-permission="['SYSTEM', 'ADMIN', 'USER']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteReport(scope.row)"
            v-permission="['SYSTEM', 'ADMIN']"
            >删除</el-button
          >
          <el-button
            size="mini"
            @click="view(scope.row)"
            v-permission="['SYSTEM', 'ADMIN']"
            >查看详情</el-button
          >

          <el-button
            size="mini"
            type="danger"
            @click="withdraw(scope.row)"
            v-permission="['USER']"
            >撤回</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页v-show="total > 0" -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageCurrent"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="total"
    ></el-pagination>
    <!-- 新增 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="addOpen"
      @close="addOpenClose"
    >
      <el-form
        :model="addForm"
        label-width="80px"
        :rules="addFormRules"
        ref="addFormRef"
      >
        <el-form-item label="报告标题" prop="title">
          <el-input v-model="addForm.title"></el-input>
        </el-form-item>
        <el-form-item label="报告内容" prop="information">
          <el-input type="textarea" v-model="addForm.information"></el-input>
        </el-form-item>
        <el-form-item label="上传附件">
          <el-upload
            v-if="addForm.annex == ''"
            :multiple="false"
            :show-file-list="false"
            action="#"
            :auto-upload="true"
            :http-request="uploadReport"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
          <el-button size="small" type="info" v-if="addForm.annex != ''"
            >已经上传</el-button
          >
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAddForm">确定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑 -->
    <el-dialog title="编辑" :visible.sync="editOpen" @close="editOpenClose">
      <el-form :model="editForm" label-width="100px" ref="editFormRef">
        <el-form-item label="报告标题" prop="title">
          <el-input v-model="editForm.title"></el-input>
        </el-form-item>
        <el-form-item
          label="处理结果"
          prop="result"
          v-permission="['SYSTEM', 'ADMIN']"
        >
          <el-select v-model="editForm.result">
            <el-option label="通过" value="0"></el-option>
            <el-option label="驳回" value="1"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="报告状态" prop="status">
                    <el-select v-model="editForm.status">
                        <el-option label="未处理" value="0"></el-option>
                        <el-option label="已处理" value="1"></el-option>
                    </el-select>
                </el-form-item>-->
        <el-form-item label="上传附件">
          <el-upload
            :multiple="false"
            :show-file-list="false"
            action="#"
            :auto-upload="true"
            :http-request="uploadReport"
          >
            <el-button size="small" type="primary" v-if="editForm.annex == ''"
              >点击上传</el-button
            >
            <el-button size="small" type="info" v-if="editForm.annex != ''"
              >已经上传</el-button
            >
          </el-upload>
        </el-form-item>
        <el-form-item label="报告内容" prop="information">
          <el-input type="textarea" v-model="editForm.information"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEditForm">确定</el-button>
      </div>
    </el-dialog>
    <!-- 查看详情 -->
    <el-dialog title="查看" :visible.sync="viewOpen">
      <el-form :model="editForm" label-width="100px" ref="editFormRef">
        <el-form-item label="报告标题" prop="title">{{
          editForm.title
        }}</el-form-item>
        <el-form-item label="处理结果" prop="result">
          <span v-if="editForm.result == 0">通过</span>
          <span v-if="editForm.result == 1">驳回</span>
        </el-form-item>
        <el-form-item label="报告状态" prop="status">
          <span v-if="editForm.status == 0">未处理</span>
          <span v-if="editForm.status == 1">已处理</span>
        </el-form-item>
        <el-form-item label="上传附件">
          <el-upload
            :multiple="false"
            :show-file-list="false"
            action="#"
            :auto-upload="true"
            :http-request="uploadReport"
          >
            <!-- <el-button size="small" type="primary" v-if="editForm.annex == ''">点击上传</el-button> -->
            <el-button
              size="small"
              v-if="editForm.annex != ''"
              @click="viewAnnex"
              >查看附件</el-button
            >
          </el-upload>
        </el-form-item>
        <el-form-item label="报告内容" prop="information">{{
          editForm.information
        }}</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEditForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  selectReport,
  saveReport,
  updateReport,
  deleteReport,
  uploadFile,
} from "@/api/report";
export default {
  data() {
    return {
      // 表格数据
      tableData: [],
      // 表格加载
      loading: false,
      // 表格条数
      total: undefined,
      // 查询参数
      queryParams: {
        title: undefined,
        creator: undefined,
        pageCurrent: 1,
        pageSize: 10,
      },
      // 弹出框标题
      dialogTitle: "",
      // 弹出框开关
      addOpen: false,
      addForm: {
        annex: "",
        information: "",
        title: "",
      },
      editEorm: {},

      // 表单校验规则
      addFormRules: {
        title: [{ required: true, message: "请输入报告标题", tagger: "blur" }],
        information: [
          { required: true, message: "请输入报告内容", tagger: "blur" },
        ],
      },
      editId: "",
      username: "",
      editOpen: false,
      editForm: {},
      dateRange: [],
      viewOpen: false,
      role: "",
    };
  },
  methods: {
    // 分页==========================================================
    handleSizeChange(pageSizeVal) {
      this.queryParams.pageSize = pageSizeVal;
      this.getList();
    },
    handleCurrentChange(pageCurrentVal) {
      this.queryParams.pageCurrent = pageCurrentVal;
      this.getList();
    },
    // 获取数据
    async getList() {
      this.loading = true;
      let res = await selectReport({
        ...this.queryParams,
        dateRange: this.dateRange,
      });
      if (res.code == 200) {
        this.tableData = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      }
    },
    // 查看详情
    view(row) {
      this.viewOpen = true;
      this.editForm = JSON.parse(JSON.stringify(row));
    },
    // 查看附件
    viewAnnex() {
      window.open("http://" + this.editForm.annex, "_blank");
    },
    // 添加
    handleAdd() {
      this.dialogTitle = "添加";
      this.addOpen = true;
      this.editId = "";
      this.addForm = {
        annex: "",
        information: "",
        title: "",
      };
    },
    // 编辑
    update(row) {
      this.editOpen = true;
      this.editId = row.id;
      this.editForm = JSON.parse(JSON.stringify(row));
      console.log(row);
    },
    // 取消编辑
    editOpenClose() {
      this.$refs.editFormRef.resetFields();
    },
    // 编辑提交
    async submitEditForm() {
      let res = await updateReport({ ...this.editForm, id: this.editId });
      if (res.code == 200) {
        this.getList();
        this.$message.success(res.msg);
        this.editOpen = false;
      }
    },
    // 添加提交表单
    async submitAddForm() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (valid) {
          let res = await saveReport(this.addForm);
          if (res.code == 200) {
            this.addOpen = false;
            this.$message.success(res.msg);
            this.getList();
          }
        }
      });
    },
    // 取消添加
    addOpenClose() {
      this.$refs.addFormRef.resetFields();
    },
    // 删除
    async deleteReport(row) {
      const confirm = await this.$confirm(
        "此操作将永久删除, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirm !== "confirm") {
        return this.$message.info("已取消删除");
      }
      // 确定删除
      deleteReport(row.id).then((res) => {
        if (res.code !== 200) {
          this.$message.error("删除失败");
        } else {
          this.$message.success("删除成功");
          this.getList();
        }
      });
    },
    // 用户撤回
    withdraw(row) {
      deleteReport(row.id).then((res) => {
        if (res.code !== 200) {
          this.$message.error("撤回失败");
        } else {
          this.$message.success("撤回成功");
          this.getList();
        }
      });
    },
    // 上传报告
    uploadReport(file) {
      let obj = new FormData();
      obj.append("file", file.file);
      obj.append("userId", null);
      uploadFile(obj).then((res) => {
        if (res.code == 200) {
          this.addForm.annex = res.data.data;
          this.editForm.annex = res.data.data;
          this.$message.success(res.msg);
        }
      });
    },

    // 搜索
    handleQuery() {
      this.queryParams.pageCurrent = 1;
      this.getList();
    },
    // 搜索重置
    resetQuery() {
      this.dateRange = [];
      this.$refs.queryForm.resetFields();
      this.getList();
    },
  },
  mounted() {
    this.getList();
    let roles = JSON.parse(sessionStorage.getItem("roles"));
    this.username = roles.name;
    this.role = roles.username;
  },
};
</script>
